package com.slz3.business.module.goodsManagement.gooBrand.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooBrand.domain.GooBrand;
import com.slz3.business.module.goodsManagement.gooBrand.domain.GooBrandFactory;
import com.slz3.business.module.goodsManagement.gooBrand.steward.GooBrandSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class GooBrandServiceImpl implements GooBrandService {

    @Override
    public GooBrand save(GooBrand gooBrand) {
        return Repositorys.gooBrand.save(GooBrandFactory.initBaseFields(gooBrand));
    }

    @Override
    public Page<GooBrand> page(MultiValueMap<String, String> params) {
        Page<GooBrand> gooBrandPage = Repositorys.gooBrand.findAll(
                GooBrandSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return gooBrandPage;
    }

    @Override
    public GooBrand modify(GooBrand gooBrandNew) {
        return Repositorys.gooBrand.saveAndFlush(GooBrandFactory.initBaseFields(gooBrandNew));
    }

    @Override
    public Optional<GooBrand> single(UUID uuid) {
        return Repositorys.gooBrand.findById(uuid);
    }

    @Override
    public List<GooBrand> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooBrand.findAllById(uuids);
    }

    @Override
    public List<GooBrand> delete(List<GooBrand> gooBrandList) {
        gooBrandList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooBrandFactory.initBaseFields(item);
        });
        return Services.gooBrandTransactional.batchModify(gooBrandList);
    }

}
